package user_manager

import (
	"GraduationProject/public/db"
	"GraduationProject/public/models"
	"GraduationProject/webserver/controllers/util"
	"github.com/gin-gonic/gin"
)

// todo: 修改用户状态
func ChangeUserStatus(c *gin.Context) {
	if _, err := util.AdminFilter(c); err != nil {
		return
	}

	userID, err := util.PostFormInt(c, "user_id")
	if err != nil {
		return
	}

	status, err := util.PostFormInt(c, "status")
	if err != nil {
		return
	}

	if status != models.UserStatusRegister &&
		status != models.UserStatusNormal &&
		status != models.UserStatusBand {
		c.JSON(200, gin.H{
			"status":  10001,
			"message": "无此用户状态",
		})
		return
	}

	if db.DB.Model(&models.User{}).
		Where("`id` = ? and `authorization` <> ?",
			userID, models.UserAuthAdministrator).
		Update("status", status).RowsAffected == 0 {
		c.JSON(200, gin.H{
			"status":  10002,
			"message": "此用户ID不存在",
		})
		return
	}

	c.JSON(200, gin.H{
		"status":  10000,
		"message": "success",
	})
}
